package com.eoner.managerlibrary.system;

import android.text.TextUtils;

import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.commonbean.config.SysConfigBean;
import com.eoner.commonbean.config.SystemConfigBean;
import com.eoner.commonbean.config.SystemStartBean;
import com.eoner.commonbean.config.SystemTabBean;
import com.eoner.commonbean.config.SystemVersionBean;
import com.eoner.managerlibrary.BuildConfig;
import com.eoner.managerlibrary.system.callback.SystemConfigCallBack;
import com.eoner.managerlibrary.system.callback.SystemConfigFansUrlCallBack;
import com.eoner.managerlibrary.system.request.SystemConfigRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
* 系统配置管理
* */
public class SystemConfigStorage {

    private final static String QUALIFICATIONS_CERTIFICATE_KEY = "qualifications_certificate";
    private final static String MAIN_BOTTOM_TAB = "main_bottom_tab";

    private SystemConfigRequest mConfigRequest;

    //版本更新提示
    private SystemVersionBean version;
    //启动页
    private SystemStartBean appStartInfo;
    //证照信息
    private List<String>licenses;

    //滑动验证码是否开启
    private boolean isOpenSlipPageCode;
    //是否开启开启微信快捷登录
    private boolean isOpenWechatQuickLogin;
    //是否显示素材圈
    private boolean isShowMaterial;
    //神策开关
    private boolean isOpenSence;

    //升级会员H5
    private String fansUrl;
    //服务协议
    private String serviceRuleUrl;
    //消息中心
    private String messageCenterUrl;
    //隐私协议
    private String privacyRuleUrl;
    //积分扫码在用，积分兑换金币公告
    private String integralExchangeUrl;
    //h5页面-账号注销
    private String accountLoginOffUrl;
    //身份证识别查看示例，多处在用
    private String ocrExampleUrl;
    // 清关了解实名认证，多处在用
    private String crossBorderExampleUrl;
    //h5页面-关于我们
    private String aboutUrl;
    //h5页面-帮助中心
    private String helpCenterUrl;
    //h5页面-下载链接
    private String downLoadUrl;

    //是否第一次发起请求
    private boolean isFirstRequest;
    // 葵花籽下载地址
    private String b_android_apk_url;
    // vip 银卡会员跳转
    private String member_ship_url;
    // 我的收入是否显示
    private boolean open = false;

    //------------------新公共数据--------------------
    //注册协议
    private String registerWordUrl;
    //隐私协议
    private String privateWordUrl;
    //参展URL
    private String applyUrl;
    //工作人员
    private String workerUrl;
    //志愿者
    private String volunteerUrl;
    //更新地址
    private String updateUrl;
    //身份类型
    private String customerType;
    //手机号码
    private String mobile;

    public String getRegisterWordUrl() {
        return registerWordUrl;
    }

    public String getPrivateWordUrl() {
        return privateWordUrl;
    }

    public String getApplyUrl() {
        return applyUrl;
    }
    public String getWorkerUrl() {
        return workerUrl;
    }
    public String getVolunteerUrl() {
        return volunteerUrl;
    }
    public String getCustomerType() {
        return customerType;
    }

    public String getMobile() {
        return mobile;
    }

    public String getUpdateUrl() {
        return updateUrl;
    }

    public boolean isFirstRequest() {
        return isFirstRequest;
    }

    public void setFirstRequest(boolean firstRequest) {
        isFirstRequest = firstRequest;
    }

    public static SystemConfigStorage instance;
    public static SystemConfigStorage getInstance() {
        synchronized (SystemConfigStorage.class) {
            if (instance == null) {
                instance = new SystemConfigStorage();
            }
        }
        return instance;
    }

    private SystemConfigStorage() {
        isFirstRequest = true;
        mConfigRequest = new SystemConfigRequest();
    }

    private SystemConfigRequest getConfigRequest() {
        if (mConfigRequest == null) {
            mConfigRequest = new SystemConfigRequest();
        }
        return mConfigRequest;
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                     对象/集合
    //
    /////////////////////////////////////////////////////////////////////////////////

    public SystemVersionBean getVersion() {
        return version;
    }

    public SystemStartBean getAppStartInfo() {
        return appStartInfo;
    }

    public List<String> getLicenses() {
        if (licenses != null && licenses.size() > 0) {
            return licenses;
        } else {
            String licensesList = SPUtils.getInstance().getString(QUALIFICATIONS_CERTIFICATE_KEY);
            if (!TextUtils.isEmpty(licensesList)&&licensesList.length()>7){
                licenses =  new Gson().fromJson(licensesList,new TypeToken<List<String>>() {}.getType());
            }
            return licenses;
        }
    }

    public SystemTabBean getMainBottomTab() {
            SystemTabBean tabBean = null;
            String tabStr = SPUtils.getInstance().getString(MAIN_BOTTOM_TAB);
            if (!TextUtils.isEmpty(tabStr)&&tabStr.length()>7){
                tabBean =  new Gson().fromJson(tabStr, SystemTabBean.class);
            }
            return tabBean;
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                     开关字段
    //
    /////////////////////////////////////////////////////////////////////////////////

    public boolean isOpenSlipPageCode() {
        return isOpenSlipPageCode;
    }

    public boolean isOpenWechatQuickLogin() {
        return isOpenWechatQuickLogin;
    }

    public boolean isShowMaterial() {
        return isShowMaterial;
    }

    public boolean isOpenSence() {
        return isOpenSence;
    }

    /////////////////////////////////////////////////////////////////////////////////
    //
    //                      h5/url
    //
    /////////////////////////////////////////////////////////////////////////////////

    public String getFansUrl() {
        return fansUrl;
    }

    public String getServiceRuleUrl() {
        return serviceRuleUrl;
    }

    public String getMessageCenterUrl() {
        return messageCenterUrl;
    }

    public String getPrivacyRuleUrl() {
        return privacyRuleUrl;
    }

    public String getIntegralExchangeUrl() {
        return integralExchangeUrl;
    }

    public String getAccountLoginOffUrl() {
        return accountLoginOffUrl;
    }

    public String getOcrExampleUrl() {
        return ocrExampleUrl;
    }

    public String getCrossBorderExampleUrl() {
        return crossBorderExampleUrl;
    }

    public String getAboutUrl() {
        return aboutUrl;
    }

    public String getHelpCenterUrl() {
        return helpCenterUrl;
    }

    public String getDownLoadUrl() {
        return downLoadUrl;
    }

    public String getB_android_apk_url() {
        return b_android_apk_url;
    }

    public String getMember_ship_url() {
        return member_ship_url;
    }

    /**
     * 获取配置信息
     * 调用规则:
     * 1、启动广告
     * 2、登录、退出
     * 3、前后切换
     * 4、回到首页
     * 5、首页下拉刷新的时候
    * */
    public void getSystemConfig(final SystemConfigCallBack configCallBack) {
        getConfigRequest().requestSystemConfig(new SystemConfigCallBack() {
            @Override
            public void onSuccess(SysConfigBean configBean) {
                //TODO:临时保存配置(1)
                SystemConfigStorage.getInstance().saveOrUpdateSysConfig(configBean);
                if (configCallBack != null) {
                    configCallBack.onSuccess();
                }
            }

            @Override
            public void onSuccess() {
                //......

            }

            @Override
            public void onFailed(String errMsg) {
                if (configCallBack != null) {
                    configCallBack.onFailed(errMsg);
                }
            }
        });
    }

    /**
    * 如果没有fansurl，去获取vip粉丝url
    * */
    public void getFansUrlIfNone(final SystemConfigFansUrlCallBack callBack) {
        if (SystemConfigStorage.getInstance().getFansUrl()!=null && SystemConfigStorage.getInstance().getFansUrl().length() > 0) {
            if (callBack != null) {
                callBack.onSuccess(SystemConfigStorage.getInstance().getFansUrl());
            }
            return;
        }
        getConfigRequest().requestSystemConfig(new SystemConfigCallBack() {
            @Override
            public void onSuccess(SystemConfigBean configBean) {
                //TODO:临时保存配置(2)
                SystemConfigStorage.getInstance().saveOrUpdateConfig(configBean);
                if (callBack != null) {
                    callBack.onSuccess(SystemConfigStorage.getInstance().getFansUrl());
                }
            }

            @Override
            public void onSuccess() {
                //......
            }

            @Override
            public void onFailed(String errMsg) {
                if (callBack != null) {
                    callBack.onSuccess("");
                }
            }
        });
    }

    /**
     * 保存数据
     * */
    private void saveOrUpdateConfig(SystemConfigBean configBean) {
        if (configBean.getData() != null) {

            //版本更新提示
            version = configBean.getData().getVersion();
            //启动页
            appStartInfo = configBean.getData().getApp_start_info();
            //证照信息/营业许可证图片集合
            licenses = configBean.getData().getLicenses();
            SPUtils.getInstance().put(QUALIFICATIONS_CERTIFICATE_KEY,new Gson().toJson(configBean.getData().getLicenses()));
            SPUtils.getInstance().put(MAIN_BOTTOM_TAB,new Gson().toJson(configBean.getData().getIndex_footer_tab()));

            //滑动验证码是否开启
            isOpenSlipPageCode = configBean.getData().isIs_open_slippage_code();
            //是否开启开启微信快捷登录
            isOpenWechatQuickLogin = configBean.getData().isIs_open_wechat_quick_login();
            //是否显示素材圈
            isShowMaterial = configBean.getData().isIs_show_material();
            //神策开关
            isOpenSence = configBean.getData().isS_is_open_sence();
            //升级会员H5
            fansUrl = configBean.getData().getFans_url();
            //服务协议
            serviceRuleUrl = configBean.getData().getService_rule_url();
            messageCenterUrl = configBean.getData().getMessage_center_url();
            //隐私协议
            privacyRuleUrl = configBean.getData().getPrivacy_rule_url();
            //积分扫码在用，积分兑换金币公告
            integralExchangeUrl = configBean.getData().getIntegral_exchange_url();
            //h5页面-账号注销
            accountLoginOffUrl = configBean.getData().getAccount_login_off_url();
            //身份证识别查看示例，多处在用
            ocrExampleUrl = configBean.getData().getOcr_example_url();
            // 清关了解实名认证，多处在用
            crossBorderExampleUrl = configBean.getData().getCross_border_example_url();
            //h5页面-关于我们
            aboutUrl = configBean.getData().getAbout_url();
            //h5页面-帮助中心
            helpCenterUrl = configBean.getData().getHelp_center_url();
            //h5页面-下载链接
            downLoadUrl = configBean.getData().getDown_load_url();
            // B端下载
            b_android_apk_url = configBean.getData().getB_android_apk_url();
            member_ship_url = configBean.getData().getMember_ship_url();
        }
    }

    /**
     * 保存数据
     * */
    private void saveOrUpdateSysConfig(SysConfigBean configBean) {
        registerWordUrl = configBean.registerWordUrl;
        privateWordUrl = configBean.privateWordUrl;
        applyUrl = configBean.applyUrl;
        workerUrl = configBean.workerUrl;
        volunteerUrl = configBean.volunteerUrl;
        updateUrl = configBean.updateUrl;
        customerType = configBean.customerType;
        mobile = configBean.mobile;

        //版本更新提示
        SystemVersionBean versionBean = new SystemVersionBean();
        versionBean.setUpdate_status(configBean.update);
        versionBean.setResource_url(configBean.updateUrl);
        versionBean.setIntroduce(configBean.updateMsg);
        versionBean.setVersion_number(BuildConfig.VERSION_NAME);
        version = versionBean;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }
}
